/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.portal.tags  
 */
package com.dwusoft.platform.template;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.sf.json.JSONObject;

import com.apache.client.InfoReleaseCoreCleint;
import com.apache.database.model.Page;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;

/**
 * description:  消息通知标签
 * @author Hou Dayu 创建时间：2015-8-29  
 * 
 */
@SuppressWarnings("serial")
public class DynamicInfoTag extends TagSupport {

	private String doCode;//枚举代码
	private String pageSize;//记录数
	private String sortCode;//动态表代码
	private String tabName;//主子表代码
	private String sqlQuery;//查询条件
	private String attrName;//属性名
	private String infoId;

	public int doStartTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			Map<String, Object> params = getMethodParams(request);
			if (StrUtil.isNotNull(sortCode)) {
				params.put("sortCode", sortCode);
			}
			if (StrUtil.isNotNull(tabName)) {
				params.put("tabName", tabName);
			}
			json2map1(params, request);
			switch (InfoMethodEnum.getName(doCode)) {
			case List:
				Object objs = InfoReleaseCoreCleint.getInstance().selectInfo(params);
				request.setAttribute(attrName, objs);
				break;
			case Pages:
				params.put("pageSize", StrUtil.doNull(pageSize, "10"));
				params.put("pageIndex", StrUtil.doNull(String.valueOf(params.get("pageIndex")), "1"));
				Object page = InfoReleaseCoreCleint.getInstance().selectInfo(params);
				gotoPageInfo(page, request);
				break;
			case View:
				Object obj = InfoReleaseCoreCleint.getInstance().selectSingle(params);
				if (null != obj) {
					request.setAttribute(attrName, obj);
				}
				break;
			case Sublist:
				Object subObjs = InfoReleaseCoreCleint.getInstance().selectMasterSlaveInfo(params);
				request.setAttribute(attrName, subObjs);
				break;
			case Subpages:
				params.put("pageSize", StrUtil.doNull(pageSize, "10"));
				params.put("pageIndex", StrUtil.doNull(String.valueOf(params.get("pageIndex")), "1"));
				Object subPage = InfoReleaseCoreCleint.getInstance().selectMasterSlaveInfo(params);
				gotoPageInfo(subPage, request);
				break;
			case Subview:
				Object subobj = InfoReleaseCoreCleint.getInstance().selectMasterSlaveSingle(params);
				if (null != subobj) {
					request.setAttribute(attrName, subobj);
				}
				break;
			case Default:
				break;
			}
		} catch (Exception e) {
			return super.SKIP_BODY;
		}
		return super.EVAL_BODY_INCLUDE;
	}

	private void gotoPageInfo(Object obj, HttpServletRequest request) {
		if (null != obj) {
			Page page = (Page) obj;
			request.setAttribute(attrName, page.getPageObjects());
			request.setAttribute("pageSize", page.getPageSize());
			request.setAttribute("pageIndex", page.getPageNeeded());
			request.setAttribute("total", page.getCount());
		}
	}

	/**
	 * json转map
	 */
	private void json2map1(Map<String, Object> params, HttpServletRequest request) {
		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
		if (StrUtil.isNotNull(sqlQuery)) {
			JSONObject jsonObject = JSONObject.fromObject(sqlQuery);
			Map<String, Object> map1 = JSONObject.fromObject(jsonObject);
			String selectUser = String.valueOf(map1.get("selectUser"));
			if ("true".equals(selectUser) && null != loginUser) {
				params.put("createUser", loginUser.getUserEname());
			}
			map1.remove("selectUser");
			params.putAll(map1);
		} else {
			if (null != loginUser) {
				params.put("createUser", loginUser.getUserEname());
			}
		}
	}

	@SuppressWarnings("rawtypes")
	private Map<String, Object> getMethodParams(HttpServletRequest request) {
		Map<String, Object> bodyMap = new HashMap<String, Object>();
		Set keSet = request.getParameterMap().entrySet();
		for (Iterator itr = keSet.iterator(); itr.hasNext();) {
			Map.Entry me = (Map.Entry) itr.next();
			Object ok = me.getKey();
			Object ov = me.getValue();
			String[] value = new String[1];
			if (ov instanceof String[]) {
				value = (String[]) ov;
			} else {
				value[0] = ov.toString();
			}
			if (value.length > 0) {
				if (String.valueOf(ok).startsWith("like_") && StrUtil.isNotNull(value[0])) {
					bodyMap.put(String.valueOf(ok).replaceAll("like_", ""), "like '%" + value[0] + "%'");
				} else if (String.valueOf(ok).startsWith("between_") && StrUtil.isNotNull(value[0])) {
					String[] str = value[0].split(":");
					bodyMap.put(String.valueOf(ok).replaceAll("between_", ""), "between " + str[0] + " and " + str[1]);
				} else {
					bodyMap.put(ok.toString(), value[0]);
					request.setAttribute(ok.toString(), value[0]);
				}
			}
		}
		if (StrUtil.isNotNull(infoId)) {
			request.setAttribute("infoId", infoId);
		}
		String sysPass = ConfigUtil.getInstance().getValueByKey("push_to_dwusoft_pass");
		bodyMap.put("sysPass", sysPass);
		return bodyMap;
	}

	public String getDoCode() {
		return doCode;
	}

	public void setDoCode(String doCode) {
		this.doCode = doCode;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getSortCode() {
		return sortCode;
	}

	public void setSortCode(String sortCode) {
		this.sortCode = sortCode;
	}

	public String getTabName() {
		return tabName;
	}

	public void setTabName(String tabName) {
		this.tabName = tabName;
	}

	public String getSqlQuery() {
		return sqlQuery;
	}

	public void setSqlQuery(String sqlQuery) {
		this.sqlQuery = sqlQuery;
	}

	/**
	 * description:  主子表代码(如:主表,子表)
	 */
	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}

	public String getInfoId() {
		return infoId;
	}

	public void setInfoId(String infoId) {
		this.infoId = infoId;
	}

}
